package com.qf.service.impl;

import com.qf.entity.Product;
import com.qf.entity.ProductExample;
import com.qf.entity.Shop;
import com.qf.entity.ShopExample;
import com.qf.entity.custom.ProductCustom;
import com.qf.mapper.ProductMapper;
import com.qf.mapper.custom.ProductCustomMapper;
import com.qf.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ZhangYubin
 * @date: 2020/7/24 9:17
 * @description:
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductCustomMapper productCustomMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 根据搜索次数降序查询product表前三条数据
     * @return 返回List<ProductCustom>的数据
     */
    @Override
    public List<ProductCustom> getProductOrderByCount() {
        return productCustomMapper.selectProductByCount();
    }



    /**
     * 用户搜索，商品名关键词
     * @param search 商品名搜索关键词
     * @return 搜索成功返回true和Product集合数据 失败返回false
     */
    @Override
    public List<Product> selecProductByNameKey(String search) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        if (search != null && search!=""){
            criteria.andProductNameLike("%"+search+"%");
            List<Product> products = productMapper.selectByExample(example);
            if (products != null){
                for (Product product :products) {
                    boolean b = modifyProductSearch(product);
                    if (b =false){
                        return null;
                    }
                }
                return products;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     * 当查询时就增加点击productCount
     * @param product 传入product实体类
     * @return 增加成功返回true 失败返回false
     */
    @Override
    public boolean modifyProductSearch(Product product) {
        Integer productCount = product.getProductCount();
        if (productCount != null) {
            int num = ++productCount;
            product.setProductCount(num);
            int i = productMapper.updateByPrimaryKeySelective(product);
            if (i != 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
